import logging
from dataclasses import dataclass

from src.application.order.uow import OrderUoW
from src.application.proxy.enums import ProxyStatus
from src.application.user import dto
from src.application.user.converters import convert_active_user_entity_to_dto

logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class UpdateProxyStatus:
    proxy_id: int
    status: ProxyStatus


class UpdateProxyStatusHandler:
    def __init__(
        self,
        *,
        uow: OrderUoW,
    ) -> None:
        self._uow = uow

    async def __call__(self, command: UpdateProxyStatus) -> None:
        proxy = await self._uow.proxy_repo.acquire_proxy_by_id(command.proxy_id)
        proxy.status = command.status

        try:
            await self._uow.proxy_repo.update_proxy(proxy)
        except Exception as err:
            await self._uow.rollback()
            raise err

        await self._uow.commit()
